home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: Philippe Verdy <100105.3120@compuserve.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Is it OK to delete const *type pointers?
- Date: 7 Apr 1996 22:39:40 GMT
- Organization: CompuServe Incorporated
- Message-ID: <4k9g7c$fl9@arl-news-svc-5.compuserve.com>
- NNTP-Posting-Host: ad04-110.compuserve.com
-
- Enno Sandner <enno@intellektik.informatik.th-darmstadt.de> s'Θcrit :
- > Acme Instant Dehydrated Boulder Kit wrote:
- > >
- > > Some compilers let me do this, others do not. What I would like to know is,
- > > what does Stroustrup think? I.e. is it written somewhere in the ARM (I checked
- > > but can't find it) or in the draft standard? If somoene could e-mail me a
- > > quote or a pointer to a place where I can read where it says such a thing
- > > should or should not be allowed, I would appreciate it!
- > >
- > > const int* array= new int[30];
- > > delete[] array;
- > >
- > > MSVC says "can not delete const*"
- > > Some versions of G++ also don't let me do this.
- > >
- > > Is deleting an object technically considered changing that object's value?
- > >
- >
- > The standard doesn't specify this (5.3.5.4):
- > ... It is unspecified whether the deletion of an object changes its
- > value. ...
- >
- > According to the DWP a dtor can be invoked even for a const-object
- > (12.4 class.dtor). Thus I would say the above code should compile.
- >
- > Enno
-
- The following compiles under BC++ 4.0, 4.5, and 5.0:
- int const *ptr = new int(1);
- int const *arr = new int[10];
- main()
- {
- delete ptr;
- delete[] arr;
- }
-
- Being const does not mean the object is read-only. Nor does
- it mean that its value won't change. Nor does it mean that
- it cannot be deleted.
- It only forbids writing to it directly without an
- explicit const_cast<>. This cast however is an operator
- which can be overriden to enforce const-ness, by returning
- a non-const copy of the referenced object.
-
- Const-ness is a security helper, which also avoids unnecessary
- invocation of copy constructors, while specifying more
- precisely the intended objects interface and the set of
- operations which can occur on them.
-